using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Ink;
using System.Windows;
using System.Xml.Serialization;
using WozLib.UI.Ink.SketchObjects;


namespace WozLib.UI.Ink.SketchCommands
{
  public class ChangeSelectionSketchCommand : SketchCommand
  {

    [XmlElement("PreviousTargetWozObjects")]
    public List<SketchObject> PreviousTargetWozObjects
    {
      get
      {
        return _PreviousTargetWozObjects;
      }
      set
      {
        _PreviousTargetWozObjects = new List<SketchObject>(value);
      }
    } private List<SketchObject> _PreviousTargetWozObjects;

    public ChangeSelectionSketchCommand()
    {
    }

    public ChangeSelectionSketchCommand(IEnumerable<SketchObject> nextWozObjects, IEnumerable<SketchObject> previousTargetWozObjects)
      : base(nextWozObjects)
    {
      _PreviousTargetWozObjects = new List<SketchObject>(previousTargetWozObjects);
    }

    public override void Redo()
    {
      if (this.TargetWozObjects != null && this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        StrokeCollection strokes = new StrokeCollection();
        List<UIElement> uies = new List<UIElement>();
        foreach (SketchObject wozObject in this.TargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {

            if (iWozObject is Stroke)
            {
              strokes.Add(iWozObject as Stroke);
            }
            else if (iWozObject is UIElement)
            {
              uies.Add(iWozObject as UIElement);
            }
          }
        }

        this.WozClientCommandReferences.InkCanvas.Select(strokes, uies);
      }
    }

    public override void Undo()
    {
      if (this.PreviousTargetWozObjects != null && this.WozClientCommandReferences != null && this.WozClientCommandReferences.InkCanvas != null)
      {
        StrokeCollection strokes = new StrokeCollection();
        List<UIElement> uies = new List<UIElement>();
        foreach (SketchObject wozObject in this.PreviousTargetWozObjects)
        {
          ISketchObject iWozObject = wozObject.GetIWozObject(this.WozClientCommandReferences.WozObjectRegistry);

          if (iWozObject.GetProperty(WozLib.UI.Ink.SketchProperties.WozPropertyType.BehindInk) == null)
          {
            if (iWozObject is Stroke)
            {
              strokes.Add(iWozObject as Stroke);
            }
            else if (iWozObject is UIElement)
            {
              uies.Add(iWozObject as UIElement);
            }
          }
        }

        this.WozClientCommandReferences.InkCanvas.Select(strokes, uies);
      }
    }

  }
}
